//
//  UIView+FQDExtension.m
//  FakeQDaily
//
//  Created by mewxiv on 8/28/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "UIView+FQDExtension.h"

@implementation UIView (FQDExtension)

- (UIImage *)captureViewHierarchy
{
    return [self captureViewHierarchyInRect:self.bounds];
}

- (UIImage *)captureViewHierarchyInRect:(CGRect)rect
{
    UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0);
    [self drawViewHierarchyInRect:rect afterScreenUpdates:NO];
    UIImage *captureImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return captureImage;
}

- (UIImage *)captureViewAfterScreenUpdates:(BOOL)afterUpdates clipRect:(CGRect)clipRect
{
    UIGraphicsBeginImageContextWithOptions(clipRect.size, YES, 0);
    CGRect drawRect = CGRectMake(-clipRect.origin.x, -clipRect.origin.y, self.bounds.size.width, self.bounds.size.height);
    [self drawViewHierarchyInRect:drawRect afterScreenUpdates:afterUpdates];
    UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return snapshot;
}

@end
